package com.own.component.common.base.entity.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.own.component.common.base.entity.po.BaseModifiedPo;
import com.own.constant.ConstantDateFormatPattern;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.FieldNameConstants;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * BaseModifiedVo
 *
 * @author chenxueli
 * @date 2022-09-10 12:21:00
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@FieldNameConstants
public class BaseModifiedVo<T extends BaseModifiedPo> extends BaseCreateVo<T> implements Serializable {

    @Schema(title = "更新人用户名")
    @JsonInclude(JsonInclude.Include.NON_DEFAULT)
    private Long updateUserId;

    @Schema(title = "更新人名称")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String updateUserName;

    @Schema(title = "更新时间")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonFormat(pattern = ConstantDateFormatPattern.Pattern.DATETIME, timezone = "GMT+8")
    private LocalDateTime gmtModified;

    /**
     * 构造方法
     *
     * @param entity 原对象
     */
    public BaseModifiedVo(@NonNull T entity) {
        super(entity);
        this.updateUserId = entity.getUpdateUserId();
        this.updateUserName = entity.getUpdateUserName();
        this.gmtModified = entity.getGmtModified();
    }

    /**
     * 转po对象
     *
     * @param entity 原
     * @return po对象
     */
    @Override
    public T toPo(T entity) {
        if (entity != null) {
            entity.setUpdateUserId(this.updateUserId);
            entity.setUpdateUserName(this.updateUserName);
            entity.setGmtModified(this.gmtModified);
        }
        return super.toPo(entity);
    }

}
